package com.fit.piris.dao.message;

import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.fit.piris.entities.message.Message;
import com.fit.piris.entities.message.priority.MessagePriority;

public class MessageDAOImpl implements MessageDAO {

	@Inject
	private Session session;

	public boolean save(Message newMessage) {
		if (newMessage != null) {
			session.save(newMessage);
			return true;
		}
		return false;
	}

	public boolean delete(Message Message) {
		if (Message != null) {
			session.delete(Message);
			return true;
		}
		return false;
	}

	public boolean update(Message Message) {
		if (Message != null) {
			session.update(Message);
			return true;
		}
		return false;
	}

	public List<Message> findAll() {
		return session.createCriteria(Message.class).list();
	}

	public Message findByID(Long id) {
		if (id != null) {
			return (Message) session.get(Message.class, id);
		}
		return null;
	}

	public boolean deleteByID(Long id) {
		if (id != null) {
			Message m = findByID(id);
			if (m != null) {
				session.delete(m);
				return true;
			}
			return false;
		}
		return false;
	}

	public List<Message> findAllForPriority(MessagePriority messagePriority) {
		if (messagePriority!=null) {
			return (List<Message>) session.createCriteria(Message.class).add(Restrictions.eq("messagePriority.id", messagePriority.getId()));
		}
		return null;
		
	}
	
}
